package clone;

import java.io.*;

/**
 * author : yeswater
 * date : 2023/7/11
 */

public class CloneTest {

    public static void main(String[] args) {
        test1();
        // test2();
    }

    /* 浅拷贝 */
    public static void test1() {
        // 原始对象
        User source = new User();
        source.setUsername("admin");
        source.setAge(18);
        source.setHobby(new Hobby("游泳"));
        // clone()实现浅拷贝
        try {
            User target = (User) source.clone();
            // 打印各自的值
            System.out.println("===修改前===");
            System.out.println(source);
            System.out.println(target);
            // 修改值后再打印
            target.getHobby().setHobby("跑步");
            System.out.println("===修改后===");
            System.out.println(source);
            System.out.println(target);
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    /* 深拷贝 */
    public static void test2() {
        // 原始对象
        User source = new User();
        source.setUsername("admin");
        source.setAge(18);
        source.setHobby(new Hobby("游泳"));
        // 序列化实现深拷贝
        try {
            // 将原始对象写入字节流
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(source);
            oos.close();
            // 从字节流中读取对象，进行反序列化
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            User target = (User) ois.readObject();
            // 打印各自的值
            System.out.println("===修改前===");
            System.out.println(source);
            System.out.println(target);
            // 修改值后再打印
            target.getHobby().setHobby("跑步");
            System.out.println("===修改后===");
            System.out.println(source);
            System.out.println(target);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}

class User implements Cloneable, Serializable {
    private String username;

    private Integer age;

    // 只有一个String类型的value属性
    private Hobby hobby;

    // 浅拷贝 : implements Cloneable ,重写 clone()
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", hobby=" + hobby +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Hobby getHobby() {
        return hobby;
    }

    public void setHobby(Hobby hobby) {
        this.hobby = hobby;
    }
}

class Hobby implements Serializable {
    String hobby;

    public Hobby() {
    }

    public Hobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Hobby{" +
                "hobby='" + hobby + '\'' +
                '}';
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}